#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


typedef struct INDATA Indata;
typedef struct TREE_NODE TreeNode;
struct INDATA{
	char letter;	//
	char lc;
	char rc;
};

struct TREE_NODE{
	char letter;
	_Bool exist;
	TreeNode *left;
	TreeNode *right;
};

_Bool checkSame(TreeNode* t1[26],TreeNode* t2[26]){
	int i=0;
	for(i=0;i<26;i++){
		if( t1[i]->exist != t2[i]->exist ) return false;
		if( NULL == t1[i]->left &&  NULL == t1[i]->right &&
			NULL == t2[i]->left &&	NULL == t2[i]->right )continue;

		else if( NULL == t1[i]->left && NULL != t1[i]->right &&
				 NULL == t2[i]->left && NULL != t2[i]->right &&
					t1[i]->right->letter == t2[i]->right->letter)continue;

		else if( NULL == t1[i]->left && NULL != t1[i]->right &&
				 NULL != t2[i]->left && NULL == t2[i]->right &&
					t1[i]->right->letter == t2[i]->left->letter )continue;

		else if( NULL != t1[i]->left && NULL == t1[i]->right &&
				 NULL != t2[i]->left && NULL == t2[i]->right &&
					t1[i]->left->letter == t2[i]->left->letter )continue;

		else if( NULL != t1[i]->left && NULL == t1[i]->right &&
				 NULL == t2[i]->left && NULL != t2[i]->right &&
					t1[i]->left->letter == t2[i]->right->letter )continue;

		else if( NULL != t1[i]->left && NULL != t1[i]->right &&
				 NULL != t2[i]->left && NULL != t2[i]->right &&
					t1[i]->left->letter == t2[i]->left->letter && 
					t1[i]->right->letter == t2[i]->right->letter )continue;

		else if( NULL != t1[i]->left && NULL != t1[i]->right &&
				 NULL != t2[i]->left && NULL != t2[i]->right &&
					t1[i]->left->letter == t2[i]->right->letter && 
					t1[i]->right->letter == t2[i]->left->letter )continue;

		else{
			return false;
		}
	}
	return true;
}
/*
	for(int i=0;i<26;i++){
		if ( t1[i]->left->letter == t2[i]->left->letter &&
			t1[i]->right->letter == t2[i]->right->letter )
				continue;
		else if ( t1[i]->left->letter == t2[i]->right->letter &&
			t1[i]->right->letter == t2[i]->left->letter )
				continue;
		else
			return false;
	}
	return true;
*/

int main(){
	Indata data[2][26];
	TreeNode* (t[2][26]);
	
	//init TreeNode *t[2][26]
	for(int i=0;i<2;i++){
		for(int j=0;j<26;j++){
			t[i][j]=malloc(sizeof(TreeNode));
			(t[i][j])->letter='A'+j;
			(t[i][j])->left=NULL;
			(t[i][j])->right=NULL;
			(t[i][j])->exist=false;
		}
	}

	int nodes;
	char c;
	for(int i=0;i<2;i++){
		scanf("%d",&nodes);
		while( (c=getc(stdin)) != '\n' );
		for(int j=0;j<nodes;j++){
			scanf("%c %c %c",&(data[i][j].letter),&(data[i][j].lc),&(data[i][j].rc));
			while( (c=getc(stdin)) != '\n' );
			//printf("%c %c %c\n",data[i][j].letter,data[i][j].lc,data[i][j].rc);
		}
	}
	int lt_i,lc_i,rc_i;
	for(int i=0;i<2;i++){
		for(int j=0;j<nodes;j++){
			lt_i=data[i][j].letter-'A';
			t[i][lt_i]->exist=true;
			if( data[i][j].lc != '-' ){
				lc_i=data[i][j].lc - '0';
				(t[i][lt_i])->left= t[i][ data[i][lc_i].letter - 'A'];
			}else{
				(t[i][lt_i])->left=NULL;
			}
			if( data[i][j].rc != '-' ){
				rc_i=data[i][j].rc - '0';
				(t[i][lt_i])->right= t[i][ data[i][rc_i].letter - 'A'];
			}else{
				(t[i][lt_i])->right=NULL;
			}
		}
	}
	
	if (checkSame(t[0],t[1]))printf("Yes\n");
	else printf("No\n");
	return 0;
}

